package guard

import (
	"github.com/gin-gonic/gin"
	"strconv"
)

type ThemesParam struct {
	Page       int
	PageSize   int
	Filter     int
	Order      string
	Lang       string
	CategoryID int
}

type ThemesParamGuard struct{}

func Themes() *ThemesParamGuard {
	return new(ThemesParamGuard)
}

func (a *ThemesParamGuard) Guard(ctx *gin.Context) (interface{}, error) {

	var l ThemesParam

	l.Page, _ = strconv.Atoi(ctx.Query("page"))
	if l.Page == 0 {
		l.Page = 1
	}
	l.PageSize, _ = strconv.Atoi(ctx.Query("page_size"))
	if l.PageSize == 0 {
		l.PageSize = 20
	}
	l.Filter, _ = strconv.Atoi(ctx.Query("filter"))
	l.Order = ctx.Query("order")
	// TODO: 限制排序字段
	// 最新/最多人点赞
	if l.Order == "" {
		l.Order = "order"
	}
	l.Lang = ctx.Query("lang")
	if l.Lang == "" {
		l.Lang = "cn"
	}
	if l.Filter == 3 {
		l.CategoryID, _ = strconv.Atoi(ctx.Query("category_id"))
	}

	return l, nil
}

func GetThemesParam(c *gin.Context) ThemesParam {
	return c.MustGet("param").(ThemesParam)
}

type ThemesBuyParam struct {
	Page     int
	PageSize int
	Lang     string
}

type ThemesBuyParamGuard struct{}

func ThemesBuy() *ThemesBuyParamGuard {
	return new(ThemesBuyParamGuard)
}

func (a *ThemesBuyParamGuard) Guard(ctx *gin.Context) (interface{}, error) {

	var l ThemesBuyParam

	l.Page, _ = strconv.Atoi(ctx.Query("page"))
	if l.Page == 0 {
		l.Page = 1
	}
	l.PageSize, _ = strconv.Atoi(ctx.Query("page_size"))
	if l.PageSize == 0 {
		l.PageSize = 20
	}
	l.Lang = ctx.Query("lang")
	if l.Lang == "" {
		l.Lang = "cn"
	}

	return l, nil
}

func GetThemesBuyParam(c *gin.Context) ThemesBuyParam {
	return c.MustGet("param").(ThemesBuyParam)
}
